
import 'package:flutter/material.dart';

import '../../../util/ImConstant.dart';
import 'UiUtil.dart';

class LButton extends StatefulWidget {
  final String assetImage;
  final String name;
  final Function selectCallback;


  Color textColor = Colors.black54;
  Color itemBackgroundColor;
  Color itemFocusColor;
  double radius = 10;

  LButton({required this.assetImage, required this.name, required this.selectCallback,
    this.textColor = Colors.black45,
    this.itemBackgroundColor = ImConstant.IM_RESERVE_BACKGROUND_COLOR,
    this.itemFocusColor = Colors.black26,
    this.radius = 10,
    super.key});

  @override
  State<StatefulWidget> createState() => _LButtonState();
}

class _LButtonState extends State<LButton> {
  late Color itemBackgroundColor;
  bool isCancel = false;
  bool isReserve = false;

  @override
  void initState() {
    super.initState();
    itemBackgroundColor = widget.itemBackgroundColor;
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(10),
      child: Center(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            _buildIcon(),
            const SizedBox(
              height: 10,
            ),
            Text(widget.name, style: TextStyle(color: widget.textColor, fontSize: 16,
                decoration: TextDecoration.none, fontWeight: FontWeight.w300),),
          ],
        ),
      ),
    );
  }

  Widget _buildIcon() {
    return GestureDetector(
      onTapDown: (detail) {
        setState(() {
          itemBackgroundColor = widget.itemFocusColor;
          isCancel = false;
        });
      },
      onTapUp: (detail) {
        isReserve = !isReserve;
        setState(() {
          itemBackgroundColor = widget.itemBackgroundColor;
          if (isCancel) {
            return;
          }
        });
        if (!isCancel) {
          widget.selectCallback();
        }
      },
      onTapCancel: () {
        setState(() {
          itemBackgroundColor = widget.itemBackgroundColor;
          isCancel = true;
        });
      },
      child: Container(
        decoration: BoxDecoration(
          color: itemBackgroundColor,
          borderRadius: BorderRadius.all(Radius.circular(widget.radius)),
        ),
        padding: const EdgeInsets.all(10),
        child: buildSvgView(widget.assetImage, 30, 30, Colors.black54),
      ),
    );
  }
}